package com.neusoft.neuiotms.dcom.converter.hn;

import java.util.List;
import java.util.stream.Collectors;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.hn.HnInspectionHealthTaskDTO;
import com.neusoft.neuiotms.dcom.model.enums.HnInspectionTaskItemStatus;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionHealthTask;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionHealthTaskItemValue;
import com.neusoft.neuiotms.dcom.repository.hn.HnInspectionHealthTaskItemValueRepository;
import com.neusoft.neuiotms.dcom.repository.hn.HnInspectionHealthTaskRepository;

@Component
public class HnInspectionHealthTaskConverter
        extends AbstractConverter<HnInspectionHealthTask, HnInspectionHealthTaskDTO> {

    @Autowired
    private HnInspectionHealthTaskRepository hnInspectionTaskRepository;
    @Autowired
    private BaseIdGenerator hnInspectionHealthTaskCodeGenerator;
    @Autowired
    private HnInspectionHealthTaskItemValueRepository hnInspectionTaskItemValueRepository;
    @Autowired
    private HnInspectionHealthTaskItemValueConverter hnInspectionTaskItemValueConverter;
    @Autowired
    private HnInspectionHealthTaskAttachmentConverter attachmentConverter;

    @Override
    public HnInspectionHealthTask toModel(final HnInspectionHealthTaskDTO dto) {
        HnInspectionHealthTask model;
        if (dto.isNew()) {
            model = new HnInspectionHealthTask();
        } else {
            model = this.hnInspectionTaskRepository.getOne(dto.getId());
        }
        model.setCode(StringUtils.isNotBlank(dto.getCode()) ? dto.getCode()
                : this.hnInspectionHealthTaskCodeGenerator.generateCode());
        model.setActualDateTime(dto.getActualDateTime());
        model.setEndDateTime(dto.getEndDateTime());
        model.setStartDateTime(dto.getStartDateTime());
        model.setUserCode(dto.getUserCode());
        model.setUserName(dto.getUserName());
        model.setInstitutionCode(dto.getInstitutionCode());
        model.setInstitutionName(dto.getInstitutionName());
        model.setProblem(dto.getProblem());
        model.setSolution(dto.getSolution());

        final List<HnInspectionHealthTaskItemValue> items = model.getItemValues().stream().collect(Collectors.toList());
        items.forEach(t -> {
            model.getItemValues().remove(t);
            this.hnInspectionTaskItemValueRepository.delete(t);
        });
        if (CollectionUtils.isNotEmpty(dto.getItemValues())) {
            dto.getItemValues().stream().map(t -> {
                final HnInspectionHealthTaskItemValue value = this.hnInspectionTaskItemValueConverter.toModel(t);
                value.setTask(model);
                return value;
            }).forEach(model.getItemValues()::add);

            if (model.getItemValues().stream().filter(it -> it.getStatus().equals(HnInspectionTaskItemStatus.alarm))
                    .findAny().isPresent()) {
                model.setItemStatus(HnInspectionTaskItemStatus.alarm);
            } else {
                model.setItemStatus(HnInspectionTaskItemStatus.normal);
            }
        } else {
            model.setItemStatus(HnInspectionTaskItemStatus.uninspected);
        }
        model.setUpdateTime(dto.getUpdateTime());
        model.setStatus(dto.getStatus());
        return model;
    }

    @Override
    public HnInspectionHealthTaskDTO toDTO(final HnInspectionHealthTask model, final boolean forListView) {
        final HnInspectionHealthTaskDTO dto = new HnInspectionHealthTaskDTO();
        dto.setCode(model.getCode());
        dto.setCreateTime(model.getCreateTime());
        dto.setEndDateTime(model.getEndDateTime());
        dto.setId(model.getId());
        dto.setStartDateTime(model.getStartDateTime());
        dto.setInstitutionCode(model.getInstitutionCode());
        dto.setInstitutionName(model.getInstitutionName());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setUserCode(model.getUserCode());
        dto.setUserName(model.getUserName());
        dto.setActualDateTime(model.getActualDateTime());
        dto.setItemValues(this.hnInspectionTaskItemValueConverter.toListDTO(model.getItemValues()));
        dto.setProblem(model.getProblem());
        dto.setSolution(model.getSolution());
        dto.setStatus(model.getStatus());
        dto.setAttachment(this.attachmentConverter.toListDTO(model.getAttachment()));
        dto.setItemStatus(model.getItemStatus());
        return dto;
    }

}
